using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.DataControls
{
    [System.ComponentModel.Designer(typeof(UserRolesDataSourceDesigner))]
    [System.Web.UI.ToolboxData("<{0}:UserRolesDataSource runat=server/>")]
    public class UserRolesDataSource : GenericDataSource<Bll.UserRole>
    {
        protected override Acms.Bll.UserRole PopulateData(UpdateAction action, System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues)
        {
            Acms.Bll.UserRole tmp = null;
            object concurencyId = null;
            if (action == UpdateAction.Insert)
            {
                int userId = GetValue<int>("UserId", values);
                int roleId = GetValue<int>("RoleId", values);
                int siteId = GetValue<int>("SiteId", values);
                tmp = Acms.Bll.UserRole.CreateInstance(userId, roleId, siteId);
            }
            else
            {
                int id = GetValue<int>("Id", keys);
                tmp = Acms.Bll.UserRole.GetById(id);
            }

            if (tmp != null)
            {
                tmp.CheckConcurent(concurencyId);
                if (RequiresUpdate<DateTime>("Start", values, oldValues))
                    tmp.Start = GetValue<DateTime>("Start", values);
                if (RequiresUpdate<DateTime>("End", values, oldValues))
                    tmp.End = GetValue<DateTime>("End", values);
            }

            return tmp;
        }

        protected override int Delete(Acms.Bll.UserRole item)
        {
            item.End = DateTime.Now;
            item.Update();
            return -1;
        }

        protected override int SaveChanges(Acms.Bll.UserRole item)
        {
            item.Update();
            return 1;
        }
    }

    public class UserRolesDataSourceDesigner : GenericDataSourceDesigner<Bll.UserRole>
    {

        protected override Acms.Bll.UserRole GetDesignTimeItem(int index)
        {
            Acms.Bll.UserRole tmp = Acms.Bll.UserRole.CreateInstance();
            return tmp;
        }
    }
}
